//
//  STPraticeViewController.m
//  KeyframeAndGroupAnimation
//
//  Created by Stan on 2017-02-19.
//  Copyright © 2017 stan. All rights reserved.
//

#import "STPraticeViewController.h"

@interface STPraticeViewController ()
@property(strong,nonatomic)CALayer *maskLayer;
@property (weak, nonatomic) IBOutlet UIImageView *backgroudImageView;

@end

@implementation STPraticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置遮罩层position
    self.maskLayer.position = self.view.center;
    
    //    self.view.layer.mask = self.maskLayer;
    self.backgroudImageView.layer.mask = self.maskLayer;
    //        遮罩层添加动画效果
    [self.maskLayer addAnimation:[self maskAni] forKey:nil];
}


- (CAKeyframeAnimation *)maskAni{
    //        放大缩小视图，keypath使用bounds
    CAKeyframeAnimation *maskAni = [CAKeyframeAnimation animationWithKeyPath:@"bounds"];
    //        动画时间
    maskAni.duration = 0.75;
    //        动画延迟0.5秒播放
    maskAni.beginTime = CACurrentMediaTime() + 0.5;
    
    
    //        设置关键帧动画的数值
    CGRect startRect = self.maskLayer.frame;
    
    CGRect tempRect = CGRectMake(0, 0, 100, 100);
    
    CGRect finalRect = CGRectMake(0, 0, 2000, 2000);
    maskAni.values = @[[NSValue valueWithCGRect:startRect],[NSValue valueWithCGRect:tempRect],[NSValue valueWithCGRect:finalRect]];
    
    
    //        设置关键帧动画的运动节奏
    maskAni.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
    
    
    //        动画播放结束后是否移除动画
    maskAni.removedOnCompletion = NO;
    
    //        动画填充模式：kCAFillModeForwards:当动画结束后，layer会一直保持着动画最后的状态
    maskAni.fillMode = kCAFillModeForwards;
    
    return maskAni;
    
    
}


//懒加载
- (CALayer *)maskLayer{
    if (!_maskLayer) {
        _maskLayer = [[CALayer alloc] init];
        _maskLayer.bounds = CGRectMake(0, 0, 50, 50);
        
        _maskLayer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"logo"].CGImage);
    }
    return _maskLayer;
}

@end
